package com.qire.manhua.activity;

import android.os.Bundle;

import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.CustomEvent;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.constant.Navigation;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.NoticeEventBus.NoticeEvent;
import com.qire.common.utils.NoticeEventBus.EventSubscriber;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.activity.helper.ComicReaderRecordHelper;
import com.qire.manhua.activity.helper.ComicReaderTouchHelper;
import com.qire.manhua.controller.ComicReaderController;
import com.qire.manhua.databinding.ActivityComicReaderBinding;
import com.qire.manhua.dialog.ComicReaderLoadDialog;
import com.qire.manhua.model.entity.BulletScreenInfo;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.entity.ComicBookEntity.DirectoryInfo;
import com.qire.manhua.model.viewModel.impl.ComicReaderViewModel;

import androidx.annotation.Nullable;

@RouteMapping(path = Navigation.Page.ComicReaderPage)
public class ComicReaderActivity extends BaseActivity<ActivityComicReaderBinding> {

    @Autowired
    ComicBookEntity comicBookEntity;

    @Autowired(injector = EventControllerInjector.class)
    ComicReaderController controller;

    @Autowired(injector = ViewModelInjector.class)
    ComicReaderViewModel viewModel;

    /** 触控事件辅助助手，用于管理手势辅助功能，入点中间呼出菜单，上下则翻页 */
    ComicReaderTouchHelper touchHelper;

    /** 漫画阅读器记录助手，用于统计记录漫画阅读位置相关的统计工作，如进度显示，上报人气等 */
    ComicReaderRecordHelper recordHelper;

    @Override
    public int layoutRid() {
        return R.layout.activity_comic_reader;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 判断是否有漫画书被传递过来，如果没有漫画书则退出页面不在继续，漫画实体通过注入传递。
        if(comicBookEntity == null) {
            ToastHelper.showSnackBar("漫画数据异常", Prompt.ERROR);
            finish();
            return;
        }
        
        // 唤起加载遮盖弹窗
        new ComicReaderLoadDialog(this).show();

        // 构建扩展Touch事件处理助手
        touchHelper = new ComicReaderTouchHelper(viewModel, controller);

        // 构建漫画阅读记录助手
        recordHelper = new ComicReaderRecordHelper(viewModel, viewDataBinder().comicReaderListView);

        // 传递事件控制器到ViewModel,为适配View加载相关子控件的事件提供处理控制器
        viewModel.onAddition(controller, viewDataBinder());

        // 传递 ViewModel 到事件控制器，为事件控制器相关操作提供支持
        controller.onAddition(viewModel);

        // 加载页面数据
        viewModel.loadComicBook(comicBookEntity);

        // 检查是否激活了新人优惠，如果激活了则显示否则不显示等到解锁余额不足时在检查显示
        viewModel.welfareCheck(false);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(recordHelper != null) {
            recordHelper.onDestroy();
            recordHelper = null;
        }
    }

    @Override
    protected void registerObserver() {
        super.registerObserver();
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.SELECT_CHAPTER, selectChapterEventSubscriber);
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.USED_NEWCOMER_REWARD, usedNewcomerRewardEventSubscriber);
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.BULLET_SCREEN_EDIT_SEND, bulletScreenSendEventSubscriber);
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.BULLET_SCREEN_EDIT_CANCEL, bulletScreenCancelEventSubscriber);
    }

    @Override
    protected void unregisterObserver() {
        super.unregisterObserver();
        NoticeEventBus.NOTICE.unSubscribeEvent(CustomEvent.SELECT_CHAPTER);
        NoticeEventBus.NOTICE.unSubscribeEvent(CustomEvent.USED_NEWCOMER_REWARD);
        NoticeEventBus.NOTICE.unSubscribeEvent(CustomEvent.BULLET_SCREEN_EDIT_SEND);
        NoticeEventBus.NOTICE.unSubscribeEvent(CustomEvent.BULLET_SCREEN_EDIT_CANCEL);
    }

    public void reportComicPopular() {
        recordHelper.reportComicPopular();
    }

    private EventSubscriber<DirectoryInfo> selectChapterEventSubscriber = (NoticeEvent<DirectoryInfo> event) -> {
        viewModel.gotoChapterBy(this, event.getEventData());
        return true;
    };

    private EventSubscriber<DirectoryInfo> usedNewcomerRewardEventSubscriber = event -> {
        viewModel.removeFirstBuyCoinViewToRoot();
        return true;
    };

    private EventSubscriber<BulletScreenInfo> bulletScreenSendEventSubscriber = (NoticeEvent<BulletScreenInfo> event) -> {
        viewModel.setBulletScreen(event.getEventData());
        controller.bulletScreenSendBtnOnClick(null);
        return true;
    };
    private EventSubscriber<BulletScreenInfo> bulletScreenCancelEventSubscriber = (NoticeEvent<BulletScreenInfo> event) -> {
        viewModel.setBulletScreen(event.getEventData());
        return true;
    };

}
